package com.itheima.shengchanxiaofei2;

/*
生产者
 */
public class Cooker extends Thread {
    private Desk desk;

    public Cooker() {
    }

    public Cooker(Desk desk) {
        this.desk = desk;
    }

    public Desk getDesk() {
        return desk;
    }

    public void setDesk(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        //    1.死循环
        while (true){
            //    2.同步代码块
            synchronized (desk.getLOCK()){
                //    3.判断是否到数了
                if(desk.getCount()==0){
                    break;
                }else {
                    //    4.判断是否该吃|生产了
                   if(!desk.isFlag()){
                     //造
                       desk.setFlag(true);
                       System.out.println("厨师正在做第"+ desk.getCount()+"个");
                       desk.getLOCK().notifyAll();//叫吃货吃
                   }else{
                       //等着
                       try {
                           desk.getLOCK().wait();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                }
            }
        }
    }
}
